package com.atl.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResponse<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;
    //用于参数校验
    private BaseResponse(Builder<T> builder) {
        this.code = builder.code;
        this.msg = builder.msg;
        this.data = builder.data;
    }

    public BaseResponse() {
        this.code = ErrorCode.SUCCESS.getCode();
        this.msg = ErrorCode.SUCCESS.getMessage();
    }

    public BaseResponse(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public BaseResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public BaseResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static BaseResponse errorResult(int code, String msg) {
        BaseResponse result = new BaseResponse();
        return result.error(code, msg);
    }

    public static BaseResponse okResult() {
        BaseResponse result = new BaseResponse();
        return result;
    }

    public static BaseResponse okResult(int code, String msg) {
        BaseResponse result = new BaseResponse();
        return result.ok(code, null, msg);
    }

    public static BaseResponse okResult(Object data) {
        BaseResponse result = setAppHttpCodeEnum(ErrorCode.SUCCESS, ErrorCode.SUCCESS.getMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    public static BaseResponse errorResult(ErrorCode enums) {
        return setAppHttpCodeEnum(enums, enums.getMessage());
    }

    public static BaseResponse errorResult(ErrorCode enums, String msg) {
        return setAppHttpCodeEnum(enums, msg);
    }

    public static BaseResponse setAppHttpCodeEnum(ErrorCode enums) {
        return okResult(enums.getCode(), enums.getMessage());
    }

    private static BaseResponse setAppHttpCodeEnum(ErrorCode enums, String msg) {
        return okResult(enums.getCode(), msg);
    }

    public BaseResponse<?> error(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public BaseResponse<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public BaseResponse<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        return this;
    }

    public BaseResponse<?> ok(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static class Builder<T> {
        private int code;
        private String msg;
        private T data;

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> message(String message) {
            this.msg = message;
            return this;
        }

        public Builder<T> data(T data) {
            this.data = data;
            return this;
        }
        public BaseResponse<T> build() {
            return new BaseResponse<>(this);
        }
    }
}
